package quq.missq.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.VolleyError;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import quq.missq.BaseActivity;
import quq.missq.Constants;
import quq.missq.R;
import quq.missq.adapter.MyContractorAdapter;
import quq.missq.beans.Author;
import quq.missq.beans.ContractorBean;
import quq.missq.utils.UserTools;
import quq.missq.utils.VolleyTool;

/**
 * Created by River on 8/11/15.
 * ta的关注
 */
public class TaContractorActivity extends BaseActivity implements VolleyTool.HTTPListener, View.OnClickListener {

    private ListView mListView;
    private MyContractorAdapter adapter;
    private ArrayList<Author> authors;
    private FrameLayout mFl_empty;
    private TextView tv_content1;
    private TextView tv_content2;
    private TextView mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_contractor_activity);
        initView();
        initData();
        initListener();
    }

    private void initView() {
        mListView = (ListView) findViewById(R.id.listView);
        findViewById(R.id.back).setOnClickListener(this);
        mTitle = (TextView) findViewById(R.id.tv_title);
        String username = getIntent().getExtras().getString("username");
        mTitle.setText(username+"的关注");
        mFl_empty = (FrameLayout) findViewById(R.id.fl_empty);
        tv_content1 = (TextView) findViewById(R.id.tv_content1);
        tv_content2 = (TextView) findViewById(R.id.tv_content2);
    }

    private void initData() {
        adapter = new MyContractorAdapter(this);
        authors = new ArrayList<>();
        adapter.setData(authors);
        mListView.setAdapter(adapter);
        loadNetWorkData();
    }

    private void initListener() {
        if (getIntent().getBooleanExtra("tag", true))
            mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("author", adapter.getData().get(position));
                    intent.putExtras(bundle);
                    setResult(RESULT_OK, intent);
                    TaContractorActivity.this.finish();
                }
            });
        else
     /*       uid = getIntent().getExtras().getInt("uid");
        username = getIntent().getExtras().getString("username");*/
            mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent();
                    intent.setClass(TaContractorActivity.this, TaPageActivity.class);

                    Bundle bundle = new Bundle();
                    bundle.putInt("uid", adapter.getItem(position).getId());
                    bundle.putString("username", adapter.getItem(position).getNkname());
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });
    }

    private void loadNetWorkData() {
        Map<String, String> params = new HashMap<>();
        params.put("uid", getIntent().getExtras().getInt("uid")+"");
        params.put("pn", "1");
        params.put("maxResults", "200");
        VolleyTool.get(this, Constants.TA_CONTRACTOR_URL, params, this, Constants.MY_CONTRACTOR, ContractorBean.class);
    }

    @Override
    public <T> void onResponse(T t, int requestCode) {
        if (requestCode == Constants.MY_CONTRACTOR) {
            ContractorBean bean = (ContractorBean) t;
            int size = bean.getData().getResults().size();
            if(size == 0){

                mFl_empty.setVisibility(View.VISIBLE);
                    tv_content1.setText("ta没有关注任何内容... ...");
            }else {
                mFl_empty.setVisibility(View.GONE);
            }
            adapter.getData().addAll(bean.getData().getResults());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error, int requestCode) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.back:
                finish();
                break;
        }
    }
}
